package cn.innosmart.aq.view.fragment;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.ReminderListAdapter;
import cn.innosmart.aq.bean.Reminder;
import cn.innosmart.aq.reminder.ReminderManager;
import cn.innosmart.aq.util.FragmentManagerTool;
import cn.innosmart.aq.util.SharedUtil;

public class ReminderListFragment extends BaseFragment {
    private ListView lvReminder;
    private TextView tvEmpty;
    private Toolbar toolbar;
       private View.OnClickListener onNavigationClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActivity().onBackPressed();
        }
    };

    private Menu menus;
    private boolean isModify;
    private ArrayList<Reminder> reminders = new ArrayList<>();
    private ReminderListAdapter reminderListAdapter;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Reminder reminder = (Reminder) v.getTag();
            switch (v.getId()) {
                case R.id.tv_delete:
                    if (reminder.isEnable = true){
                        ReminderManager.cancelAlarm(getActivity(), reminder);
                    }
                    SharedUtil.getInstance().removeReminder(reminder.id);
                    reminders.remove(reminder);
                    reminderListAdapter.refresh(reminders);
                    break;
                case R.id.rl_bg:
                    ReminderFragment reminderFragment = new ReminderFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt("flag", 1);
                    bundle.putParcelable("reminder", reminder);
                    reminderFragment.setArguments(bundle);
                    FragmentManagerTool.addToBackStackFragment(getActivity().getSupportFragmentManager(), reminderFragment, R.id.boxdetail_content);
                    break;
            }
        }
    };
    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_add:
                    FragmentManagerTool.addToBackStackFragment(getActivity().getSupportFragmentManager(), new ReminderFragment(), R.id.boxdetail_content);
                    break;
                case R.id.action_edit:
                    isModify = !isModify;
                    if (isModify) {
                        menus.findItem(R.id.action_add).setVisible(false);
                        menus.findItem(R.id.action_edit).setTitle(R.string.action_exit_edit);
                    } else {
                        menus.findItem(R.id.action_add).setVisible(true);
                        menus.findItem(R.id.action_edit).setTitle(R.string.edit);
                    }
                    reminderListAdapter.startModify(isModify);
                    break;
            }
            return true;
        }
    };
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Reminder reminder = (Reminder) buttonView.getTag();
            if (isChecked) {
                reminder.isEnable = true;
                ReminderManager.createAlarm(getActivity(), reminder);
            } else {
                reminder.isEnable = false;
                ReminderManager.cancelAlarm(getActivity(), reminder);
            }
            SharedUtil.getInstance().updateReminder(reminder);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        return inflater.inflate(R.layout.fragment_reminder_list, null);
    }

    private void setBar() {
        toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.toolbar_back);
        toolbar.setTitle(getString(R.string.activity_more_reminder));
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(onNavigationClickListener);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setBar();
        getData();
        lvReminder = (ListView) view.findViewById(R.id.lv_reminder);
        reminderListAdapter = new ReminderListAdapter(getActivity(), reminders);
        tvEmpty = (TextView) view.findViewById(R.id.tv_empty);
        lvReminder.setEmptyView(tvEmpty);
        reminderListAdapter.setOnClickListener(onClickListener);
        reminderListAdapter.setOnCheckedChangeListener(onCheckedChangeListener);
        lvReminder.setAdapter(reminderListAdapter);
    }

    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        getActivity().getMenuInflater().inflate(R.menu.menu_assign, menu);
        menus = menu;
        super.onCreateOptionsMenu(menu, inflater);
    }

    private void getData() {
        reminders = SharedUtil.getInstance().queryAllReminder();
    }


}
